from flask_sqlalchemy import SQLAlchemy
from dcs_web import app

db = SQLAlchemy(app)


class BaseConfig(db.Model):
    __tablename__ = 'base_config'

    key_name = db.Column(db.String(32), primary_key=True)
    key_type = db.Column(db.String(32))
    key_value = db.Column(db.String(32))


class BaseMachine(db.Model):
    __tablename__ = 'base_machine'

    tid = db.Column(db.Integer, primary_key=True)
    machine_no = db.Column(db.String(32))
    machine_ip = db.Column(db.String(32))
    factory_no = db.Column(db.String(32))
    workshop_no = db.Column(db.String(32))
    workshop_area = db.Column(db.String(32))
    spindle_count = db.Column(db.Integer)
    spindle_mode = db.Column(db.Integer)
    standard_spindle_speed = db.Column(db.Integer)
    front_roller_speed = db.Column(db.Integer)
    standard_twist = db.Column(db.Integer)
    rear_roller_speed = db.Column(db.Integer)
    dtime = db.Column(db.String(32))
    uploaded = db.Column(db.Integer)
    upload_time = db.Column(db.String(32))


class LogMachineStatus(db.Model):
    __tablename__ = 'log_machine_status'

    tid = db.Column(db.Integer, primary_key=True)
    factory_no = db.Column(db.String(32))
    workshop_no = db.Column(db.String(32))
    machine_ip = db.Column(db.String(32))
    machine_no = db.Column(db.String(32))
    status = db.Column(db.Integer)
    dtime = db.Column(db.DateTime())
    average_speed = db.Column(db.Float())
    spin_length = db.Column(db.Float())
    theory_length = db.Column(db.Float())
    actual_length = db.Column(db.Float())
    temperature = db.Column(db.Float())
    humidity = db.Column(db.Float())
    pm25 = db.Column(db.Float())
    noise = db.Column(db.Float())
    voltage = db.Column(db.Float())
    power = db.Column(db.Float())
    watt = db.Column(db.Float())
    energy = db.Column(db.Float())
    uploaded = db.Column(db.Integer)
    upload_time = db.Column(db.DateTime())


class LogSpindleSpeed(db.Model):
    __tablename__ = 'log_spindle_speed'

    tid = db.Column(db.Integer, primary_key=True)
    machine_ip = db.Column(db.String(32))
    machine_no = db.Column(db.String(32))
    spindle_no = db.Column(db.String(32))
    actual_no = db.Column(db.String(32))
    speed = db.Column(db.Integer)
    dtime = db.Column(db.DateTime())


class LogSpindleStatus(db.Model):
    __tablename__ = 'log_spindle_status'

    tid = db.Column(db.Integer, primary_key=True)
    factory_no = db.Column(db.String(32))
    machine_ip = db.Column(db.String(32))
    machine_no = db.Column(db.String(32))
    spindle_no = db.Column(db.String(32))
    actual_no = db.Column(db.String(32))
    status = db.Column(db.Integer)
    dtime = db.Column(db.DateTime())
    uploaded = db.Column(db.Integer)
    upload_time = db.Column(db.DateTime())


class RawMachineConfig(db.Model):
    __tablename__ = 'raw_machine_config'

    tid = db.Column(db.Integer, primary_key=True)
    machine_ip = db.Column(db.String(32))
    raw_data = db.Column(db.LargeBinary)
    dtime = db.Column(db.DateTime())


class RawMachineStatus(db.Model):
    __tablename__ = 'raw_machine_status'

    tid = db.Column(db.Integer, primary_key=True)
    machine_ip = db.Column(db.String(32))
    raw_data = db.Column(db.LargeBinary)
    dtime = db.Column(db.DateTime())


class RawSpindleSpeed(db.Model):
    __tablename__ = 'raw_spindle_speed'

    tid = db.Column(db.Integer, primary_key=True)
    machine_ip = db.Column(db.String(32))
    machine_no = db.Column(db.String(32))
    raw_data = db.Column(db.LargeBinary)
    offset = db.Column(db.Integer)
    count = db.Column(db.Integer)
    dtime = db.Column(db.DateTime())


class RawSpindleStatus(db.Model):
    __tablename__ = 'raw_spindle_status'

    tid = db.Column(db.Integer, primary_key=True)
    machine_ip = db.Column(db.String(32))
    machine_no = db.Column(db.String(32))
    raw_data = db.Column(db.LargeBinary)
    offset = db.Column(db.Integer)
    count = db.Column(db.Integer)
    dtime = db.Column(db.DateTime())



